package com.tebiecloud.store.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.store.client.model.entity.StoreStaff;
import com.tebiecloud.store.client.model.entity.StoreStaffWallet;
import com.tebiecloud.store.client.model.vo.DeductAmountVo;
import com.tebiecloud.store.client.model.vo.PlusAmountVo;
import com.tebiecloud.store.client.service.IStoreStaffWalletServiceClient;
import com.tebiecloud.store.server.service.StoreStaffService;
import com.tebiecloud.store.server.service.StoreStaffWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("/feign/staffwallet")
public class StaffWalletController implements IStoreStaffWalletServiceClient {

    @Autowired
    StoreStaffWalletService storeStaffWalletService;

    @Autowired
    StoreStaffService storeStaffService;

    /**
     * 获取员工钱包信息
     * @param userId //用户id
     * @return
     */
    @PostMapping("infoStoreStaffWallet")
    @Override
    public ResultBody<StoreStaffWallet> getStoreStaffWallet(@RequestParam("userId") long userId) {
        QueryWrapper<StoreStaff> storeStaffQueryWrapper = new QueryWrapper<>();
        storeStaffQueryWrapper.eq("user_id",userId);
        StoreStaff storeStaff =  storeStaffService.getOne(storeStaffQueryWrapper);
        QueryWrapper<StoreStaffWallet> storeStaffWalletQueryWrapper = new QueryWrapper<>();
        storeStaffWalletQueryWrapper.eq("store_staff_id",storeStaff.getStoreStaffId());
        StoreStaffWallet storeStaffWallet = storeStaffWalletService.getOne(storeStaffWalletQueryWrapper);
        return ResultBodyUtils.ok(storeStaffWallet);
    }

    /**
     * 扣员工钱包金额
     * @param userId 用户id
     * @param amount 扣钱总金额，单位分
     * @param maxCoin 乖乖币抵扣上限
     * @return
     */
    @PostMapping("updateDeductAmount")
    @Override
    public ResultBody<DeductAmountVo> updateDeductAmount(@RequestParam("userId") long userId,@RequestParam("amount") int amount, @RequestParam("maxCoin") int maxCoin) {
        QueryWrapper<StoreStaff> storeStaffQueryWrapper = new QueryWrapper<>();
        storeStaffQueryWrapper.eq("user_id",userId);
        StoreStaff storeStaff =  storeStaffService.getOne(storeStaffQueryWrapper);
        DeductAmountVo deductAmountVo = storeStaffWalletService.updateDeductAmount(storeStaff.getStoreStaffId(),amount,maxCoin);
        return ResultBodyUtils.ok(deductAmountVo);
    }

    /**
     * 添加员工钱包
     * @param userId 用户id
     * @param balance 增加的余额，单位分
     * @param coin 增加的乖乖币
     * @return
     */
    @PostMapping("updatePlusAmount")
    @Override
    public ResultBody<PlusAmountVo> updatePlusAmount(@RequestParam("userId") long userId, @RequestParam("balance") int balance, @RequestParam("coin") int coin) {
        QueryWrapper<StoreStaff> storeStaffQueryWrapper = new QueryWrapper<>();
        storeStaffQueryWrapper.eq("user_id",userId);
        StoreStaff storeStaff =  storeStaffService.getOne(storeStaffQueryWrapper);
        PlusAmountVo plusAmountVo = storeStaffWalletService.updatePlusAmount(storeStaff.getStoreStaffId(),balance,coin);
        return ResultBodyUtils.ok(plusAmountVo);
    }
}
